package com.syq;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.syq.pojo.Logs;
import com.syq.utils.EsUtil;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.IndicesClient;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.junit.Test;

import java.util.Date;

/**
 * @ClassName: HomeWork
 * @Description: TODO
 * @Create by: syq
 * @Date: 2022/8/8 20:19
 */


public class HomeWork {
    private static String index="sms-logs-index";
    private ObjectMapper objectMapper = new ObjectMapper();
    public static void main(String[] args) throws Exception{
        RestHighLevelClient client = EsUtil.getClient();
        IndicesClient indices = client.indices();
        CreateIndexRequest createIndexRequest=new CreateIndexRequest(index);
        Settings.Builder settings=Settings.builder().put("number_of_shards","5").put("number_of_replicas","1");
        createIndexRequest.settings(settings);
        XContentBuilder mapping= JsonXContent.contentBuilder()
                        .startObject()
                        .startObject("properties")
                        .startObject("createDate").field("type","date").field("format","yyyy-MM-dd HH:mm:ss").endObject()
                        .startObject("sendDate").field("type","date").field("format","yyyy-MM-dd HH:mm:ss").endObject()
                        .startObject("longCode").field("type","long").endObject()
                        .startObject("mobile").field("type","long").endObject()
                        .startObject("corpName").field("type","text").field("analyzer","ik_max_word").endObject()
                        .startObject("smsContent").field("type","text").field("analyzer","ik_max_word").endObject()
                        .startObject("state").field("type","integer").endObject()
                        .startObject("operatorId").field("type","integer").endObject()
                        .startObject("province").field("type","keyword").endObject()
                        .startObject("ipAddr").field("type","ip").endObject()
                        .startObject("replyTotal").field("type","float").endObject()
                        .startObject("fee").field("type","integer").endObject()
                        .endObject()
                        .endObject();
        createIndexRequest.mapping(mapping);
        CreateIndexResponse createIndexResponse = indices.create(createIndexRequest, RequestOptions.DEFAULT);
    }
    @Test
    public void add() throws Exception {
        RestHighLevelClient client = EsUtil.getClient();
        Logs log=new Logs(new Date(),new Date(),106900167892L,18624377601L,"中国联通增值服务","欢迎使用中国联通",0,2,"黑龙江","10.169.29.5",3.2f,100);
        String json = objectMapper.writeValueAsString(log);
        IndexRequest indexRequest=new IndexRequest(index);
        indexRequest.source(json, XContentType.JSON);
        IndexResponse index = client.index(indexRequest, RequestOptions.DEFAULT);
        System.out.println(index.status());
    }
}
